package com.ldzy.user;

import com.ldzy.common.excption.UnauthorizedException;
import com.ldzy.user.config.JwtProperties;
import com.ldzy.user.pojo.User;
import com.ldzy.user.utils.JwtTool;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequiredArgsConstructor
public class DemoController {
    private final JwtProperties jwtProperties;
    private final JwtTool jwtTool;

    @PostMapping("/user/login")
    public String login(@RequestBody User user, HttpServletResponse response){
        if ("dwh".equals(user.getName())&&"123".equals(user.getPwd())){
            //生成token,返回
            String token=null;
            token= jwtTool.createToken(Long.valueOf(user.getId()), jwtProperties.getTokenTTL());
            return token;
        }else {
            throw new UnauthorizedException("用户名或密码不正确！！");
        }

    }


    @GetMapping("/user/hello")
    public String hello(){
        return "user 9003";
    }
}
